﻿using System;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
using System.Configuration;
using Microsoft.Xrm.Sdk;

namespace Founder.PKURG.CRM
{
    public class AdminCrmConnect
    {
        /// <summary>
        /// isIFD=false;UserName=administrator;Password=Founder@123;Domain=test;OrgServiceUri=http://172.25.20.82/PKURG/XRMServices/2011/Organization.svc
        /// </summary>
        private string _ConnectionString;

        public string ConnectionString
        {
            get { return _ConnectionString; }
            set 
            {
                _ConnectionString = value;
               // InitService();
            }
        }
        
        private ConfigInfo _ConfigurationInfo;

        public ConfigInfo ConfigurationInfo
        {
            get { return _ConfigurationInfo; }
            set { _ConfigurationInfo = value; }
        }
        

        public AdminCrmConnect()
        {
            _ConnectionString = System.Configuration.ConfigurationManager.AppSettings["CRMConfig"];
        }

        public AdminCrmConnect(string connectionString)
        {
            _ConnectionString = connectionString;
        }
        
        private  OrganizationServiceProxy _OrgService;
        public IOrganizationService OrgService
        {
            get
            {
                if (_OrgService==null)
                {
                    InitService();
                }
                return _OrgService;
            }
        }

        private void InitService()
        {
            ConfigurationInfo = ConfigInfo.GetConfigInfoV2(_ConnectionString);

            string isIFD = ConfigurationInfo.ISIFD.ToLower().Trim();
            string orgServiceUriString = ConfigurationInfo.OrgServiceUri;

            string userName = ConfigurationInfo.UserName;
            string password = ConfigurationInfo.Password;
            string domainName = ConfigurationInfo.Domain;

            Uri orgServiceUri = new Uri(orgServiceUriString);
            ClientCredentials credentials = new ClientCredentials();

            if (isIFD == Boolean.TrueString.ToString().ToLower())
            {
                credentials.UserName.UserName = domainName + "\\" + userName;
                credentials.UserName.Password = password;
            }
            else if (isIFD == Boolean.FalseString.ToString().ToLower())
            {
                credentials.Windows.ClientCredential =
                    new System.Net.NetworkCredential(userName, password, domainName);
            }
            else
            {
                throw new Exception("IsIFD is incorrect");
            }
            _OrgService = new OrganizationServiceProxy(orgServiceUri, null, credentials, null);
        }

        private  OrganizationServiceContext _orgServiceContext;
        public  OrganizationServiceContext orgServiceContext
        {
            get
            {
                if (_orgServiceContext == null)
                    _orgServiceContext = new OrganizationServiceContext(OrgService);
                return _orgServiceContext;
            }
        }

        public void init()
        {
            _OrgService = null;
            _orgServiceContext = null;
        }
    }
}